
import os
import datetime
from textparamsparse import *
def renamefile(path, oldstr, newstr):
    """
    扫描目录内重复文件，
    :param path: 扫描路径
    :            oldstr 要替换文件名的字符串
                 newStt 心得字符串

                 如 02_1000.bin  oldstr="1000"   newstr=0250

    :return:
    """

    if os.path.exists(path) == False:
        print("Store Path :" + path + " isn't exist!")
        return None
    try:
        files = os.listdir(path)
    except FileNotFoundError:
        print("File Not FoundError")
        return None
    for fi in files:
        # file full path
        # sleep 1
        # time.sleep(1)
        fi_d = os.path.join(path, fi)
        samefilelist = None
        # is directory
        if os.path.isdir(fi_d):
            if os.path.islink(fi_d):
                continue
            """ recursion"""
            renamefile(fi_d, oldstr,newstr)


            print("dir = ", fi_d)
        # is file
        else:
            file_path, suffix, file_name = getFilePathExtend(fi_d)



            tmpfullpath = file_path + "/" + file_name

            newfullpath = tmpfullpath.replace(oldstr, newstr)
            samekey = ""
            os.rename(tmpfullpath, newfullpath)
            print(tmpfullpath)






def getFilePathExtend(fileName):
    filePath, suffix = os.path.splitext(fileName)
    filePath = os.path.dirname(fileName)
    l = len(suffix)
    fileName = os.path.split(fileName)[1]
    return filePath,  suffix[1:l + 1], fileName

if __name__ == '__main__':
    renamefile("E:/0077", "0250","1000" )